package steak.xteamy.client.mixin;

import net.minecraft.client.gui.DrawContext;
import net.minecraft.client.gui.screen.ChatScreen;
import net.minecraft.client.gui.screen.Screen;
import net.minecraft.text.Text;
import org.spongepowered.asm.mixin.Mixin;
import org.spongepowered.asm.mixin.Shadow;
import org.spongepowered.asm.mixin.Unique;
import org.spongepowered.asm.mixin.injection.At;
import org.spongepowered.asm.mixin.injection.Inject;
import org.spongepowered.asm.mixin.injection.callback.CallbackInfo;
import org.spongepowered.asm.mixin.injection.callback.CallbackInfoReturnable;
import steak.xteamy.client.VoiceChat.ChatScreenComponent;

@Mixin(ChatScreen.class)
public abstract class ChatScreenMixin extends Screen {

    @Shadow protected abstract void setInitialFocus();

    @Unique private final ChatScreen self = (ChatScreen) (Object) this;

    @Unique ChatScreenComponent chatScreenComponent = new ChatScreenComponent();

    protected ChatScreenMixin(Text title) {
        super(title);
    }

    @Inject(method = "init", at = @At("RETURN"))
    private void init(CallbackInfo info) {
        for (ChatScreenComponent.ChannelButton channelButton : chatScreenComponent.getChannelButtons()) {
            this.addSelectableChild(channelButton);
        }
    }

    @Inject(method = "render", at = @At("RETURN"))
    private void onRender(DrawContext context, int mouseX, int mouseY, float delta, CallbackInfo ci) {
        chatScreenComponent.render(context, mouseX, mouseY, delta);
    }

    @Inject(method = "resize", at = @At("RETURN"))
    private void onResize(CallbackInfo info) {
        chatScreenComponent.resize();
    }

    @Inject(method = "keyPressed", at = @At("HEAD"))
    private void onKeyPressed(int keyCode, int scanCode, int modifiers, CallbackInfoReturnable<Boolean> cir) {
        ChatScreenAccessor accessor = (ChatScreenAccessor) self;
        setFocused(accessor.chatField());
    }
}
